package com.genghis.ptas.note;

import com.genghis.ptas.classify.entity.Type;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.note.dao.PublicityNoteDao;
import com.genghis.ptas.note.entity.Note;
import com.genghis.ptas.note.service.NoteService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("/note/")
public class NoteController {
    @Autowired
    private NoteService noteService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private UserService userService;

    @Autowired
    private PublicityNoteDao publicityNoteDao;

    @RequestMapping(value = "myNote", method = {RequestMethod.GET})
    public String auditNote(HttpServletRequest request, Note note) {
        List<TypeValues> typeValuesList = classifyService.findAllTypeValue();
        List<Type> typeList = classifyService.findAllTypeList();
        List<Note> noteList = noteService.findNoteByType(note);
        request.setAttribute("noteList", noteList);
        request.setAttribute("typeValuesList", typeValuesList);
        request.setAttribute("typeList", typeList);
        return "frontstage/note/editNoteDo";
    }

    @RequestMapping(value = "listAllNote", method = {RequestMethod.POST})
    @ResponseBody
    public List<Note> listAllNoteDo(Note note) {
        return noteService.findNoteByType(note);
    }

    @RequestMapping(value = "addNoteDo", method = {RequestMethod.GET})
    public String addNote(HttpServletRequest request) {
        List<TypeValues> typeValueList = classifyService.findAllTypeValueList();
        request.setAttribute("typeValueList", typeValueList);
        return "frontstage/note/addNoteDo";
    }

    @RequestMapping(value = "addNote", method = {RequestMethod.POST})
    @ResponseBody
    public void addNoteDo(HttpServletRequest request, Note note) {
        User user = (User) request.getSession().getAttribute("user");
        note.setUserId(user.getId());
        noteService.addNote(note);
    }

    @RequestMapping(value = "editNoteDo", method = {RequestMethod.GET})
    public String editNote(HttpServletRequest request, @RequestParam("id") int id) {
        Note note = noteService.findNoteById(id);
        List<TypeValues> typeValueList = classifyService.findAllTypeValueList();
        request.setAttribute("note", note);
        request.setAttribute("typeValueList", typeValueList);
        return "frontstage/note/editNoteDo";
    }

    @RequestMapping(value = "editNote", method = {RequestMethod.POST})
    @ResponseBody
    public void editNoteDo(Note note) {
        noteService.editNote(note);
    }

    @RequestMapping(value = "delNoteDo", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delNoteDo(@RequestParam("id") String id) {
        noteService.delNote(id);
    }

    @RequestMapping(value = "publicNotes", method = {RequestMethod.DELETE})
    @ResponseBody
    public void publicNotes(@RequestParam("id") String ids) {
        noteService.publicNotes(ids);
    }

    @RequestMapping(value = "noPublicNote", method = {RequestMethod.DELETE})
    @ResponseBody
    public void noPublicNote(@RequestParam("id") String ids) {
        publicityNoteDao.delPublicityNote(ids);
    }

}
